package com.yita.demo04_project_reflect.controller;

import com.yita.demo04_project_reflect.dao.FruitDAO;
import com.yita.demo04_project_reflect.dao.Impl.FruitDAOImpl;
import com.yita.demo04_project_reflect.pojo.Fruit;

import java.util.List;
import java.util.Scanner;

public class Menu {

    Scanner input=new Scanner(System.in);
    FruitDAO fruitDAO = new FruitDAOImpl();
    //主菜单
    public int showMenu() {
        System.out.println("=================欢迎使用水果库存系统=====================");
        System.out.println("1.查看水果库存列表");
        System.out.println("2.添加水果库存信息");
        System.out.println("3.查看特定水果库存信息");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("======================================================");
        System.out.print("请选择：");

        return input.nextInt();
    }

    //查看水果库存列表
    public void showFruitList() {
        List<Fruit> fruitList = fruitDAO.getFruitList();


        if (fruitList == null) {
            System.out.println("对不起，库存为空！");
        }else {
            System.out.println("------------------------------------------------------");
            System.out.println(String.format("%s\t%-5s\t%-6s\t%-5s\t%s","编号","名称","单价","库存","备注"));
            for (Fruit fruit : fruitList) {
                System.out.println(fruit);
            }
            System.out.println("------------------------------------------------------");
        }

    }

    //添加水果库存信息
    public void addFruit() {
        System.out.print("请输入水果名称：");
        String fname=input.next();
        Fruit fruit= fruitDAO.getFruitByFname(fname);
        if (fruit == null) {
            fruit = new Fruit();
            fruit.setFname(fname);
            System.out.println("请输入水果单价:");
            int price = input.nextInt();
            fruit.setPrice(price);
            System.out.println("请输入水果库存量:");
            int count = input.nextInt();
            fruit.setFcount(count);
            System.out.println("请输入水果备注:");
            String remark = input.next();
            fruit.setRemark(remark);

            fruitDAO.addFruit(fruit);
        }else {
            System.out.println("请输入追加库存量:");
            int count = input.nextInt();
            fruit.setFcount(fruit.getFcount()+count);
            fruitDAO.updateFruit(fruit);
        }
        System.out.println("添加成功");

    }
    //查看特定水果库存信息
    public void showFruitInfo() {
        System.out.println("请输入水果名称:");
        String fname = input.next();
        Fruit fruit = fruitDAO.getFruitByFname(fname);
        if (fruit == null) {
            System.out.println("对不起，没有找到指定的水果库存记录！");
        } else {
            System.out.println("------------------------------------------------------");
            System.out.println(String.format("%s\t%-5s\t%-6s\t%-5s\t%s","编号","名称","单价","库存","备注"));
            System.out.println(fruit);
            System.out.println("------------------------------------------------------");
        }
    }

    //水果下架
    public void delFruit() {
        System.out.println("要下架的水果:");
        String fname = input.next();
        Fruit fruit = fruitDAO.getFruitByFname(fname);
        if (fruit == null) {
            System.out.println("对不起，没有找到需要下架的水果信息！");
        } else {
            System.out.println("是否确认下架？(Y/N)");
            String next = input.next();
            if ("y".equalsIgnoreCase(next)) {
                fruitDAO.delFruit(fname);
                System.out.println("下架成功!");
            }
        }

    }

    public boolean exit() {
        System.out.println("是否退出？(y/n)");
        String str=input.next();
        return !"y".equalsIgnoreCase(str);
    }
}
